package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Api(tags = "菜品相关接口")
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}", dishDTO);

        dishService.saveWithFlavor(dishDTO);

        // 清理缓存
        deleteKeys(dishDTO.getCategoryId());

        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询：{}", dishPageQueryDTO);

        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteBatch(@RequestParam List<Long> ids){
        log.info("菜品的批量删除：{}", ids);

        dishService.deleteBatch(ids);

        // 可能影响多个key， 简单操作——》 删除全部菜品缓存
        cleanRedis("dish_");

        return Result.success();
    }


    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> findById(@PathVariable Long id){
        log.info("根据id查询菜品：{}", id);

        // 查询菜品信息
        DishVO dishVO = dishService.findWithFlavor(id);

        return Result.success(dishVO);
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("list")
    public Result<List<Dish>> findByCategoryId(@RequestParam Long categoryId){
        log.info("根据分类id查询菜品：{}", categoryId);

        List<Dish> dishes = dishService.findByCategoryId(categoryId);
        return Result.success(dishes);
    }

    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}", dishDTO);

        dishService.update(dishDTO);

        // 可能影响多个key， 简单操作——》 删除全部菜品缓存
        cleanRedis("dish_");

        return Result.success();
    }

    @ApiOperation("菜品起售停售")
    @PostMapping("/status/{status}")
    public Result StartOrStop(@PathVariable Integer status, Long id){
        log.info("菜品起售停售: {}", status);

        dishService.StartOrStop(status, id);

        // 删除缓存
        Dish dish = dishService.findById(id);
        deleteKeys(dish.getCategoryId());

        return Result.success();
    }

    // 清理全部缓存数据
    private void cleanRedis(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    // 精确清理
    private void deleteKeys(Long categoryId){
        String key = "dish_" + categoryId;
        redisTemplate.delete(key);
    }
}
